package com.chixing.service.impl;


import com.chixing.entity.MongDB.Reply;
import com.chixing.repository.ReplyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class ReplyService {

    @Autowired
    private ReplyRepository replyRepository;

    /**
     * 新增回复
     */
    public Reply addReply(Reply reply) {
        reply.setCreateTime(LocalDateTime.now());
        reply.setIsDeleted(0); // 0=未删除, 1=已删除

        // ① 先保存一次，让 MongoDB 生成 ID
        Reply saved = replyRepository.save(reply);

        List<String> path = new ArrayList<>();

        // ② 构建 path & level
        if (saved.getParentId() == null || saved.getParentId().isEmpty()) {
            // ⭐ 楼主的直接回复
            saved.setLevel(0);
            path.add(saved.getPostId());  // 根为主楼ID
            path.add(saved.getId());      // 回复ID
        } else {
            // ⭐ 回复某条回复
            Reply parent = replyRepository.findById(saved.getParentId()).orElse(null);

            if (parent != null) {
                saved.setLevel(parent.getLevel() + 1);
                path.addAll(parent.getPath());  // 继承父 path
                path.add(saved.getId());        // 添加当前回复 ID
            } else {
                // 父找不到，降级为主楼直接回复
                saved.setLevel(0);
                path.add(saved.getPostId());
                path.add(saved.getId());
            }
        }

        saved.setPath(path);

        // ③ 再保存一次，更新 path & level
        return replyRepository.save(saved);
    }

    /**
     * 查询某主楼的平铺回复列表
     */
    public List<Reply> getRepliesByPostId(String postId) {
        return replyRepository.findByPostIdOrderByPathAsc(postId);
    }

    /**
     * 查询某主楼的树形楼中楼
     */
    public List<Reply> getReplyTreeByPostId(String postId) {
        List<Reply> replies = replyRepository.findByPostIdOrderByPathAsc(postId);
        return buildTree(replies);
    }

    /**
     * 构建树形楼中楼
     */
    private List<Reply> buildTree(List<Reply> replies) {
        Map<String, Reply> map = new HashMap<>();
        List<Reply> roots = new ArrayList<>();

        // 初始化 children
        for (Reply r : replies) {
            r.setChildren(new ArrayList<>());
            map.put(r.getId(), r);
        }

        // 挂载子节点
        for (Reply r : replies) {
            if (r.getParentId() == null || r.getParentId().isEmpty()) {
                roots.add(r);
            } else {
                Reply parent = map.get(r.getParentId());
                if (parent != null) {
                    parent.getChildren().add(r);
                } else {
                    // 父节点找不到，归为顶层
                    roots.add(r);
                }
            }
        }

        return roots;
    }

    /**
     * 查询用户所有回复
     */
    public List<Reply> getRepliesByUser(Long userId) {
        return replyRepository.findByUserId(userId);
    }

    /**
     * 查询酒店下所有回复
     */
    public List<Reply> getRepliesByHotel(Long hotelId) {
        return replyRepository.findByHotelId(hotelId);
    }

    /**
     * 查询订单对应的回复
     */
    public List<Reply> getRepliesByOrder(Long orderId) {
        return replyRepository.findByOrderId(orderId);
    }

    /**
     * 查询未删除的回复
     */
    public List<Reply> getActiveReplies() {
        return replyRepository.findByIsDeleted(0);
    }
}